package net.oschina.app.ui.view;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;

import net.oschina.app.R;
import net.oschina.app.interf.ResultState;
import net.oschina.app.util.CommonUtils;

/**
 * Created by wei03 on 2017/1/13.
 */

public abstract class ContentPager extends FrameLayout implements ResultState {
    private View errorView;
    private View emptyView;
    private View loadingView;
    private View networkView;
    private View successView;
    //放在接口
/*    public static final int STATE_LOADING = 0;
    public static final int STATE_ERROR = 1;
    public static final int STATE_EMPTY = 2;
    public static final int STATE_NETWORK = 3;*/
    public int state = STATE_DEFAULT;//默认为正在加载状态

    public ContentPager(Context context) {
        super(context);
        initPager();
    }

    private void initPager() {
        if (errorView == null) {
            errorView = onCreateErrorView();
            this.addView(errorView);
        }
        if (emptyView == null) {
            emptyView = onCreateEmptyView();
            this.addView(emptyView);
        }
        if (networkView == null) {
            networkView = onCreateNetWorkView();
            this.addView(networkView);
        }
        if (loadingView == null) {
            loadingView = onCreateLoadingView();
            this.addView(loadingView);
        }

        showPager();
    }

    private void showPager() {
        if (loadingView != null) {
            loadingView.setVisibility(state == STATE_LOADING ? View.VISIBLE : INVISIBLE);
        }
        if (emptyView != null) {
            emptyView.setVisibility(state == STATE_EMPTY ? VISIBLE : INVISIBLE);
        }
        if (networkView != null) {
            networkView.setVisibility(state == STATE_NETWORK ? VISIBLE : INVISIBLE);
        }
        if (errorView != null) {
            errorView.setVisibility(state == STATE_ERROR ? VISIBLE : INVISIBLE);
        }
        if (successView == null) {
            successView = onCreateSuccessView();
            if (successView != null) {
                this.addView(successView);
            } else {
                throw new IllegalArgumentException("成功界面(successView)创建失败！");
            }
        }
        if (successView != null) {
            successView.setVisibility(state == STATE_SUCCESS ? View.VISIBLE : View.INVISIBLE);
        }
    }

    public abstract View onCreateSuccessView();

    public void loadData() {
        if (state != STATE_LOADING) {
            state = STATE_DEFAULT;
        }
        if (state == STATE_DEFAULT) {
            //可用线程池优化
            new Thread(new Runnable() {
                @Override
                public void run() {
                    state = onLoadData();
//                    SystemClock.sleep(1000);
                    CommonUtils.runOnMainThread(new Runnable() {
                        @Override
                        public void run() {
                            showPager();
                        }
                    });
                }
            }).start();

        }
    }

    public abstract int onLoadData();

    private View onCreateErrorView() {
        return CommonUtils.inflate(R.layout.view_error);
    }

    private View onCreateEmptyView() {
        return CommonUtils.inflate(R.layout.view_empty);
    }

    private View onCreateNetWorkView() {
        return CommonUtils.inflate(R.layout.view_network);
    }

    private View onCreateLoadingView() {
        return CommonUtils.inflate(R.layout.view_loading);
    }
}
